﻿//页面加载
$(document).ready(function () {
    //本页面不适用ajax
    g_IsOpenAjax = false; //不开启ajax 效果
    $("#btnSave").bind("click", btnSave_click);
    $("#btnCancel").bind("click", btnCancel_click);
    $("#btnAdd").bind("click", btnAdd_click);
    $("#btnPlus").bind("click", btnPlus_click);
    $("#btnMinus").bind("click", btnMinus_click);
    $("#Dic_ProductType").bind("change", GetProducts_change);
    GetProducts_change(); //设置默认选中
    $("#txtCardCode").bind("input propertychange", txtCardCode_change); 
    $("#btnSend").bind("click", btnSend_click);

    $("#txtCardCode").focus();
})

//给用户发送验证码
function btnSend_click()
{
    var $table = $("#TableList");
    var v_count = $table.find(".contentRow").length;
    if (v_count == 0)
    {
        alert("请先选择产品！");
        return;
    }
    var v_cardCode = $("#txtCardCode").val();
    if (v_cardCode == "") {
        alert("请输入卡号！");
        return;
    }
    $.post("/Order/SaveOrder", { cardCode: v_cardCode }, function (result) {
        if (result.code == "OK") {
            alert("保存成功");
            window.location.href = "/Order/Index";
        }
        else {
            alert("保存失败，请稍后重试");
        }
    }, "json");

}

//自动获取卡号用户信息
function txtCardCode_change()
{
    $("#cardMsg").html("");
    v_cardCode = $("#txtCardCode").val();

    $.post("/VipCard/GetCardInfo", { code: v_cardCode }, function (result) {
        if (result.code == "OK") {
            //s设置卡号有效
            $("#hdValid").val("1");
            if (result.data.Dic_CardType == "108002") {
                $("#cardMsg").html("当前卡为计次卡，剩余次数：" + result.data.Balance + " 次 ,持卡人：" + result.data.RealName);
                $("#cardMsg").attr("class","m10 blue");
                //自动设置计次卡选中类型
                var v_limitedProductId = result.data.Could_Buy;
                if (result.data.Could_Buy != "") {
                    var v_loc = result.data.Could_Buy.indexOf(',');
                    if (v_loc >= 0) {
                        v_limitedProductId = v_limitedProductId.substring(0, v_loc);
                        $("#Products").val(v_limitedProductId);
                    }
                    else {
                        $("#Products").val(v_limitedProductId);
                    }
                    $("#Products").prop("disabled", true);
                    $("#Dic_ProductType").prop("disabled", true);
                }

            }
            if (result.data.Dic_CardType == "108001") {

                $("#cardMsg").html("当前卡为储值卡，剩余金额：" + result.data.Balance + " 元,持卡人：" + result.data.RealName);
                $("#cardMsg").attr("class", "m10 cf60");
                $("#Products").prop("disabled", false);
                $("#Dic_ProductType").prop("disabled", false);
            }

        }
        else {
            $("#hdValid").val("0");  //卡号无效
            $("#cardMsg").attr("class", "m10");
        }
    }, "json");
}

function btnAdd_click()
{
    var v_ProductId = $("#Products").val();
    if (v_ProductId == 0)
    {
        alert("请选择一个产品");
        return;
    }
    var v_cardCode = $("#txtCardCode").val();
    if (v_cardCode == "") {
        alert("请输入购买人卡号");
        return;
    }
    var v_isValid = $("#hdValid").val();
    if (v_isValid == "0")
    {
        alert("当前卡号无效，请重新输入卡号！");
        return;
    }
    var v_quantity = $("#txtQuantity").val();
    $.post("/Order/AddToShoppingCart", { cardCode: v_cardCode, productId: v_ProductId, quantity: v_quantity }, function (result) {
        ShowShoppingCard(result);
    }, "json");

}

function ShowShoppingCard(result)
{
    var $table = $("#TableList");
    var $template = $("#template");
    $table.find(".contentRow").remove();
    $.each(result.data, function (i, n) {
        var row = $template.clone();
        row.css("display", "");
        //数据项
        row.find("#td_checkbox").html("<input type='checkbox' name='checkbox'  class='chk' value='" + n.Pro_ID + "'/>");
        row.find("#td_Image").html("<img class='proImage m5' src='" + n.Image + "'></img>");
        row.find("#td_ProductName").html(n.ProductName);
        row.find("#td_Price").html("￥" + n.Price);
        row.find("#td_Quantity").html(n.Quantity);
        row.find("#td_Discount").html(n.Discount);
        row.find("#td_BuyPrice").html("￥" + n.BuyPrice);
        row.find("#td_Total").html("￥" + n.Total);
        //操作
        //row.find("#btnSetMenu").bind("click", { ID: n.AID }, btnSetMenu_click);
        // row.find("#btnEdit").bind("click", { ID: n.AID }, btnEdit_click);
        row.find("#btnDelete").bind("click", { ID: n.Pro_ID }, btnDelete_click);
        row.attr("class", "contentRow t_c");
        row.appendTo($table);
    });

    // 订单总价：
    $("#orderTotal").html(result.orderTotal);
}

//<td id="td_ProductName">@item.ProductName</td>
//删除单项
function btnDelete_click(e) {
    var Id = e.data.ID;
    $.post("/Order/Remove", { productId: Id }, function (result) {
        ShowShoppingCard(result);
    },"json");

}

function btnMinus_click() {
    var quantity = $("#txtQuantity");
    var curValue = parseInt(quantity.val()) - 1;
    if (curValue < 1) {
        curValue = 1;
    }
    quantity.val(curValue);
}

function btnPlus_click() {
    var quantity = $("#txtQuantity");
    var curValue = parseInt(quantity.val()) + 1;
    quantity.val(curValue);
}

//获取产品列表
function GetProducts_change()
{
    var v_select = $("#Dic_ProductType").val();

    $.post("/Order/GetProductsByType", { selectTypeId: v_select }, function (result) {
        var ddlHtml = "";
        $.each(result.data, function (i, item) {
            var item = "<option value='" + item.AID + "'>" + item.Name + "</option>";
            ddlHtml += item;
        })
        $("#Products").html(ddlHtml);
    },"json");
}

function btnSave_click() {
    //异步提交表单,表单中input元素必须设置name 属性
    $.post("/Order/Save?action=Create", $("#fmSave").serialize(), function (result) {
        if (result == "OK") {
            alert("数据保存成功!");
        }
        else {
            alert("数据保存失败，请重试!");
        }
    });
}

function btnCancel_click() {
    window.history.go(-1);
}